from common.public import *
from orm.model import Borrow,PayForm,ExpenseForm,DiscountList,ExpenseForm,PayForm
from view.baseview import BaseView
from flask import json
import time

class ApprovalView(BaseView):
    def __init__(self):
        pass

    def query(self, params):
        '''status:待审核，已审核，已作废'''
        """取得审批列表"""
        redata = []

        # 优惠单
        data = {"name": "优惠单","data": []}
        rows = None

        if params['status'] == AUDIT_STATUS_OVER:
            # 作废数据
            rows = DiscountList.select().where((DiscountList.status == AUDIT_STATUS_OVER) \
                 & ((DiscountList.leader_id == params['userid'])\
                   | (DiscountList.manager_id == params['userid'])\
                   ))\
                .order_by(DiscountList.create_time.desc()).paginate(1, 50)
        elif params['status'] == AUDIT_STATUS_PASSED:
            # 已审核
            rows = DiscountList.select().where( \
                 ((DiscountList.leader_id == params['userid']) & (DiscountList.leaderstatus == AUDIT_STATUS_PASSED) \
                   | (DiscountList.manager_id == params['userid']) & (DiscountList.managerstatus == AUDIT_STATUS_PASSED) \
                   ))\
                .order_by(DiscountList.create_time.desc()).paginate(1, 50)
        else:
            # 待审核
            rows = DiscountList.select().where((DiscountList.status == AUDIT_STATUS_WAITE)\
                 & ((DiscountList.leader_id == params['userid']) & (DiscountList.leaderstatus == AUDIT_STATUS_WAITE) \
                   | (DiscountList.manager_id == params['userid']) & (DiscountList.managerstatus == AUDIT_STATUS_WAITE) \
                   ))\
                .order_by(DiscountList.create_time.desc()).paginate(1, 50)
        for row in rows:
            mdata = {
                "form":{"id":row.id,"userid":params['userid'],"status":row.status,\
                        "leaderId":row.leader_id,"leaderstatus":row.leaderstatus,\
                        "managerId":row.manager_id,"managerstatus":row.managerstatus},
                "data":[
                        {"name": "填写日期", "value": str(row.create_time)[0:10], "type": "text"},
                        {"name": "客户名称", "value": row.customer.compname, "type": "text"},
                        {"name": "申请人", "value": row.applicant.username, "type": "text"},
                        {"name": "状态", "value": row.status, "type": "text"},
                        {"name": "主管", "value": row.leader.username + "(" + row.status+")", "type": "text"},
                        {"name": "经理", "value": row.manager.username + "(" + row.status+")", "type": "text"},
                        {"name": "优惠描述", "value": row.preferential_description, "type": "text"},
                        {"name": "截止日期", "value": str(row.end_date)[0:10], "type": "text"},
                ]
            }
            for srow in row.discountlistdetail_set:
                mdata['data'].append({"name": srow.service.name, "value": srow.servicedetail.spec + " " + srow.servicedetail.type, "type": "text"})
                mdata['data'].append({"name": "├─数量", "value": str(srow.amount) + srow.servicedetail.unit, "type": "text"})
                mdata['data'].append({"name": "├─原价", "value": "￥" +str(srow.priceb), "type": "text"})
                mdata['data'].append({"name": "└─优惠价", "value": "￥" +str(srow.pricea), "type": "text"})

            mdata['data'].append({"name": "主管", "value": row.leader.username + "(" + row.leaderstatus + ")", "type": "text"})
            mdata['data'].append({"name": "经理", "value": row.manager.username + "(" + row.managerstatus + ")", "type": "text"})

            if params['status'] == AUDIT_STATUS_WAITE:
                mdata['data'].append({"name": "通过", "value": "api/discountform/audit", "type": "button", "color": "info"})
                mdata['data'].append({"name": "作废", "value": "api/discount/remove", "type": "button", "color": "warning"})
            elif params['status'] == AUDIT_STATUS_PASSED:
                mdata['data'].append({"name": "作废", "value": "api/discount/remove", "type": "button", "color": "warning"})
            else:
                mdata['data'].append({"name": "-", "value": "-", "type": "divider"})

            data['data'].append(mdata)
        if len(data['data']) > 0:
            redata.append(data)

        # 借支单
        data = {"name": "借支单","data": []}
        rows = None
        if params['status'] == AUDIT_STATUS_OVER:
            rows = Borrow.select().where((Borrow.status == AUDIT_STATUS_OVER) \
                 & ((Borrow.approver_id == params['userid']) \
                    | (Borrow.master_id == params['userid']) \
                    | (Borrow.cashier_id == params['userid'])\
                   | (Borrow.bookkeeper_id == params['userid'])))\
                .order_by(Borrow.createdate.desc()).paginate(1, 50)
        elif params['status'] == AUDIT_STATUS_PASSED:
            rows = Borrow.select().where(  \
                ((Borrow.approver_id == params['userid']) & (Borrow.approverstatus == AUDIT_STATUS_PASSED) \
                 | (Borrow.master_id == params['userid']) & (Borrow.masterstatus == AUDIT_STATUS_WAITE) \
                 | (Borrow.cashier_id == params['userid']) & (Borrow.cashierstatus == AUDIT_STATUS_PASSED) \
                 | (Borrow.bookkeeper_id == params['userid']) & (
                             Borrow.bookkeeperstatus == AUDIT_STATUS_PASSED))) \
                .order_by(Borrow.createdate.desc()).paginate(1, 50)
        else:
            rows = Borrow.select().where((Borrow.status == AUDIT_STATUS_WAITE)\
                 & ((Borrow.approver_id == params['userid']) & (Borrow.approverstatus == AUDIT_STATUS_WAITE) \
                    | (Borrow.master_id == params['userid']) & (Borrow.masterstatus == AUDIT_STATUS_WAITE) \
                    | (Borrow.cashier_id == params['userid']) & (Borrow.cashierstatus == AUDIT_STATUS_WAITE) \
                   | (Borrow.bookkeeper_id == params['userid']) & (Borrow.bookkeeperstatus == AUDIT_STATUS_WAITE)))\
                .order_by(Borrow.createdate.desc()).paginate(1, 50)
        for row in rows:
            if row.master_id == 0:
                masterstatus = ""
            else:
                masterstatus = row.master.username + "(" + row.masterstatus + ")"
            mdata = {
                "form":{"id":row.id,"userid":params['userid'],"status":row.status,\
                        "masterid":row.master_id,"masterstatus":row.masterstatus,\
                        "approverid":row.approver_id,"approverstatus":row.approverstatus,\
                        "cashierid":row.cashier_id,"cashierstatus":row.cashierstatus,\
                        "bookkeeperid":row.bookkeeper_id,"bookkeeperstatus":row.bookkeeperstatus},
                "data":[
                        {"name": "填写日期", "value": str(row.createdate), "type": "text"},
                        {"name": "部门", "value": row.dept.name, "type": "text"},
                        {"name": "借支人", "value": row.borrower.username, "type": "text"},
                        {"name": "城市", "value": row.city, "type": "text"},
                        {"name": "贷款事由", "value": row.content, "type": "text"},
                        {"name": "借款金额", "value": "￥" + str(row.amount), "type": "text"},
                        {"name": "借款方式", "value": row.loantype, "type": "text"},
                        {"name": "还款方式", "value": row.repaytype, "type": "text"},
                        {"name": "还款日期", "value": str(row.repaydate), "type": "text"},
                        {"name": "状态", "value": row.status, "type": "text"},
                        {"name": "主管", "value": masterstatus, "type": "text"},
                        {"name": "审核人", "value": row.approver.username + "(" + row.approverstatus + ")", "type": "text"},
                        {"name": "出纳", "value": row.cashier.username + "(" + row.cashierstatus + ")", "type": "text"},
                        {"name": "会计", "value": row.bookkeeper.username + "(" + row.bookkeeperstatus + ")", "type": "text"},
                ]
            }
            for annex in json.loads(row.annex):
                mdata['data'].append({"name": annex, "value": API_BORROWFORM_ANNEX_DOWNLOAD +str(row.id) + "/" + annex, "type": "image"})
            if params['status'] == AUDIT_STATUS_WAITE:
                mdata['data'].append({"name": "通过", "value": "api/borrow/update", "type": "button", "color": "info"})
                mdata['data'].append({"name": "作废", "value": "api/borrow/delete", "type": "button", "color": "warning"})
            elif params['status'] == AUDIT_STATUS_PASSED:
                mdata['data'].append({"name": "作废", "value": "api/borrow/delete", "type": "button", "color": "warning"})
            else:
                mdata['data'].append({"name": "-", "value": "-", "type": "divider"})

            data['data'].append(mdata)
        if len(data['data']) > 0:
            redata.append(data)


        # 报销单
        data = {"name": "报销单","data": []}
        rows = None
        if params['status'] == AUDIT_STATUS_OVER:
            rows = ExpenseForm.select().where((ExpenseForm.status == AUDIT_STATUS_OVER) \
                 & ((ExpenseForm.approver_id == params['userid']) \
                    | (ExpenseForm.master_id == params['userid']) \
                    | (ExpenseForm.cashier_id == params['userid'])\
                   | (ExpenseForm.bookkeeper_id == params['userid'])))\
                .order_by(ExpenseForm.createdate.desc()).paginate(1, 50)
        elif params['status'] == AUDIT_STATUS_PASSED:
            rows = ExpenseForm.select().where(  \
                ((ExpenseForm.approver_id == params['userid']) & (ExpenseForm.approverstatus == AUDIT_STATUS_PASSED) \
                 | (ExpenseForm.master_id == params['userid']) & (ExpenseForm.masterstatus == AUDIT_STATUS_PASSED) \
                 | (ExpenseForm.cashier_id == params['userid']) & (ExpenseForm.cashierstatus == AUDIT_STATUS_PASSED) \
                 | (ExpenseForm.bookkeeper_id == params['userid']) & (
                             ExpenseForm.bookkeeperstatus == AUDIT_STATUS_PASSED))) \
                .order_by(ExpenseForm.createdate.desc()).paginate(1, 50)
        else:
            rows = ExpenseForm.select().where((ExpenseForm.status == AUDIT_STATUS_WAITE)\
                 & ((ExpenseForm.approver_id == params['userid']) & (ExpenseForm.approverstatus == AUDIT_STATUS_WAITE) \
                  | (ExpenseForm.master_id == params['userid']) & (ExpenseForm.masterstatus == AUDIT_STATUS_WAITE) \
                  | (ExpenseForm.cashier_id == params['userid']) & (ExpenseForm.cashierstatus == AUDIT_STATUS_WAITE) \
                   | (ExpenseForm.bookkeeper_id == params['userid']) & (ExpenseForm.bookkeeperstatus == AUDIT_STATUS_WAITE)))\
                .order_by(ExpenseForm.createdate.desc()).paginate(1, 50)
        print(rows)
        for row in rows:
            mdata = {
                "form":{"id":row.id,"userid":params['userid'],"status":row.status, \
                        "masterid": row.master_id, "masterstatus": row.masterstatus, \
                        "approverid":row.approver_id,"approverstatus":row.approverstatus,\
                        "cashierid":row.cashier_id,"cashierstatus":row.cashierstatus,\
                        "bookkeeperid":row.bookkeeper_id,"bookkeeperstatus":row.bookkeeperstatus},
                "data":[
                        {"name": "填写日期", "value": str(row.createdate), "type": "text"},
                        {"name": "部门", "value": row.dept.name, "type": "text"},
                        {"name": "报销人", "value": row.agent.username, "type": "text"},
                        {"name": "报销方式", "value": row.expensetype, "type": "text"},
                        {"name": "报销事由", "value": row.content, "type": "text"},
                        {"name": "报销金额", "value": "￥" + str(row.amount), "type": "text"},
                        {"name": "状态", "value": row.status, "type": "text"},
                        {"name": "主管", "value": row.master.username + "(" + row.masterstatus + ")", "type": "text"},
                        {"name": "审核人", "value": row.approver.username + "(" + row.approverstatus + ")", "type": "text"},
                        {"name": "出纳", "value": row.cashier.username + "(" + row.cashierstatus + ")", "type": "text"},
                        {"name": "会计", "value": row.bookkeeper.username + "(" + row.bookkeeperstatus + ")", "type": "text"},
                ]
            }
            for annex in json.loads(row.annex):
                mdata['data'].append({"name": annex, "value": API_EXPENSEFORM_ANNEX_DOWNLOAD +str(row.id) + "/" + annex, "type": "image"})
            if params['status'] == AUDIT_STATUS_WAITE:
                mdata['data'].append({"name": "通过", "value": "api/expenseform/update", "type": "button", "color": "info"})
                mdata['data'].append({"name": "作废", "value": "api/expenseform/delete", "type": "button", "color": "warning"})
            elif params['status'] == AUDIT_STATUS_PASSED:
                mdata['data'].append({"name": "作废", "value": "api/expenseform/delete", "type": "button", "color": "warning"})
            else:
                mdata['data'].append({"name": "-", "value": "-", "type": "divider"})

            data['data'].append(mdata)
        if len(data['data']) > 0:
            redata.append(data)

        # 付款单
        data = {"name": "付款单","data": []}
        rows = None
        if params['status'] == AUDIT_STATUS_OVER:
            rows = PayForm.select().where((PayForm.status == AUDIT_STATUS_OVER) \
                 & ((PayForm.approver_id == params['userid']) \
                    | (PayForm.master_id == params['userid']) \
                    | (PayForm.cashier_id == params['userid'])\
                   | (PayForm.bookkeeper_id == params['userid'])))\
                .order_by(PayForm.createdate.desc()).paginate(1, 50)
        elif params['status'] == AUDIT_STATUS_PASSED:
            rows = PayForm.select().where(  \
                ((PayForm.approver_id == params['userid']) & (PayForm.approverstatus == AUDIT_STATUS_PASSED) \
                 | (PayForm.master_id == params['userid']) & (PayForm.masterstatus == AUDIT_STATUS_WAITE) \
                 | (PayForm.cashier_id == params['userid']) & (PayForm.cashierstatus == AUDIT_STATUS_PASSED) \
                 | (PayForm.bookkeeper_id == params['userid']) & (
                             PayForm.bookkeeperstatus == AUDIT_STATUS_PASSED))) \
                .order_by(PayForm.createdate.desc()).paginate(1, 50)
        else:
            rows = PayForm.select().where((PayForm.status == AUDIT_STATUS_WAITE)\
                 & ((PayForm.approver_id == params['userid']) & (PayForm.approverstatus == AUDIT_STATUS_WAITE) \
                  | (PayForm.master_id == params['userid']) & (PayForm.masterstatus == AUDIT_STATUS_WAITE) \
                  | (PayForm.cashier_id == params['userid']) & (PayForm.cashierstatus == AUDIT_STATUS_WAITE) \
                   | (PayForm.bookkeeper_id == params['userid']) & (PayForm.bookkeeperstatus == AUDIT_STATUS_WAITE)))\
                .order_by(PayForm.createdate.desc()).paginate(1, 50)
        for row in rows:
            if row.master_id == 0:
                masterstatus = ""
            else:
                masterstatus = row.master.username + "(" + row.masterstatus + ")"
            mdata = {
                "form":{"id":row.id,"userid":params['userid'],"status":row.status,\
                        "masterid":row.master_id,"masterstatus":row.masterstatus,\
                        "approverid":row.approver_id,"approverstatus":row.approverstatus,\
                        "cashierid":row.cashier_id,"cashierstatus":row.cashierstatus,\
                        "bookkeeperid":row.bookkeeper_id,"bookkeeperstatus":row.bookkeeperstatus},
                "data":[
                        {"name": "填写日期", "value": str(row.createdate), "type": "text"},
                        {"name": "部门", "value": row.dept.name, "type": "text"},
                        {"name": "经办人", "value": row.agent.username, "type": "text"},
                        {"name": "付款方式", "value": row.paytype, "type": "text"},
                        {"name": "收款单位", "value": row.company.name, "type": "text"},
                        {"name": "收款人", "value": row.reciver, "type": "text"},
                        {"name": "账号", "value": row.bankno, "type": "text"},
                        {"name": "开户行", "value": row.bankname, "type": "text"},
                        {"name": "付款金额", "value": row.amount, "type": "text"},
                        {"name": "付款事由", "value": row.content, "type": "text"},
                        {"name": "状态", "value": row.status, "type": "text"},
                        {"name": "主管", "value": masterstatus, "type": "text"},
                        {"name": "审核人", "value": row.approver.username + "(" + row.approverstatus + ")", "type": "text"},
                        {"name": "出纳", "value": row.cashier.username + "(" + row.cashierstatus + ")", "type": "text"},
                        {"name": "会计", "value": row.bookkeeper.username + "(" + row.bookkeeperstatus + ")", "type": "text"},
                ]
            }
            for annex in json.loads(row.annex):
                mdata['data'].append({"name": annex, "value": API_PAYFORM_ANNEX_DOWNLOAD +str(row.id) + "/" + annex, "type": "image"})
            if params['status'] == AUDIT_STATUS_WAITE:
                mdata['data'].append({"name": "通过", "value": "api/payform/update", "type": "button", "color": "info"})
                mdata['data'].append({"name": "作废", "value": "api/payform/delete", "type": "button", "color": "warning"})
            elif params['status'] == AUDIT_STATUS_PASSED:
                mdata['data'].append({"name": "作废", "value": "api/payform/delete", "type": "button", "color": "warning"})
            else:
                mdata['data'].append({"name": "-", "value": "-", "type": "divider"})

            data['data'].append(mdata)
        if len(data['data']) > 0:
            redata.append(data)
            
            
        if len(redata) == 0:
            return super().resultSet(201, "无审批数据！",  redata)

        return super().resultSet(200, "数据取得成功！",  redata)
